const webSocket = require('ws')
const wss = new webSocket.Server({ port: 9527 })
wss.on('connection', function connection(ws) {
    ws.on('open', function open() {
        console.log('connected');
        ws.send('hello');
    });


    ws.on('message', function incoming(data) {
    
        // 广播
        wss.clients.forEach(function each(client) {
            console.log(data.toString());
            if (client.readyState == webSocket.OPEN) {
                console.log(data.toString());
                client.send(data.toString())
            }
        })
    })

    ws.on('close', function close() {
        console.log('disconnected');
    });
})

